<?php

include '../database/koneksi/Koneksi.php';
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Inspiratif
 *
 * @author BAGUS
 */
class Inspiratif {

    //put your code here
    var $tgl, $author, $inspiratif;

    public function getTgl() {
        return $this->tgl;
    }

    public function getAuthor() {
        return $this->author;
    }

    public function getInspiratif() {
        return $this->inspiratif;
    }

    public function setTgl($tgl) {
        $this->tgl = $tgl;
    }

    public function setAuthor($author) {
        $this->author = $author;
    }

    public function setInspiratif($inspiratif) {
        $this->inspiratif = $inspiratif;
    }

    public function simpanInspiratif($ins) {
        $kon = new Koneksi();
        $kon->koneksiDB();
        $data = null;
        $idS = null;
        $id = null;
        $i = 0;
        $result = mysql_query("select * from inspiratif");
        if (!$result) {
            echo 'Could not run query: ' . mysql_error();
            exit;
        }
        while ($row = mysql_fetch_array($result)) {
            $idS = $row["id_inspiratif"];
            $data[$i++] = $idS;
        }
        if (sizeof($data) == 0) {
            $id = 1;
        } else {
            $id = $this->bubbleAsc($data);
            $id++;
        }
        //masih slah di idnya..!!!
        $idFix = intval($id);
        $s = mysql_query("insert into inspiratif (id_inspiratif,pesan,tgl_update,author,status) "
                . "values('" . mysql_real_escape_string($idFix) . "','"
                . mysql_real_escape_string($this->getInspiratif()) . "','"
                . mysql_real_escape_string($this->getTgl()) . "','"
                . mysql_real_escape_string($this->getAuthor()) . "','0')");        
        if (!$s) {
            echo 'Could not run query: ' . mysql_error();
            exit;
        }
    }
    
    public function tampilInspiratif(){
        $kon = new Koneksi();
        $kon->koneksiDB();        
        $result = mysql_query("select * from inspiratif");
        if (!$result) {
            echo 'Could not run query: ' . mysql_error();
            exit;
        }
        $arrayData = null;
        $i = 0;
        while ($row = mysql_fetch_array($result)) {
           $arrayData[$i++] = $row["nama_propinsi_pemberi"];
        }
        return $arrayData;
    }
    
    public function updateStatusIns($arr){
        $kon = new Koneksi();
        $kon->koneksiDB();
        mysql_query("update inspiratif set status='0'");
        for ($i = 1; $i < strlen($arr); $i++) {
            if ($arr[$i] != null) {
                mysql_query("update inspiratif set status='1' where id_inspiratif='" + $arr[$i] + "'");
            }
        }
    }
    
    public function tampilInsUtama(){
        $kon = new Koneksi();
        $kon->koneksiDB();
        $result = mysql_query("select * from inspiratif where status='1'");
        return $result;
    }
    
    public function hapusInspiratif($arr){
        $kon = new Koneksi();
        $kon->koneksiDB();
        for ($i = 1; $i < strlen($arr); $i++) {
            if ($arr[$i] != null) {
                mysql_query("delete from inspiratif where id_inspiratif='" + $arr[$i] + "'");
            }
        }
    }
    
    function bubbleAsc($dat) {
        $tmp = 0;
        $max = 0;
        for ($a = 0; $a < sizeof($dat); $a++) {
            for ($b = $a + 1; $b < sizeof($dat); $b++) {
                if ($dat[$a] > $dat[$b]) {
                    $tmp = $dat[$a];
                    $dat[$a] = $dat[$b];
                    $dat[$b] = $tmp;
                    $max = $dat[$b];
                }
            }
        }
        return $max;
    }
}
